﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using ControleRepresentante.Data.Catalogs;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.UnitTests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.Web.Mvc;
using WebSite.Controllers;
using WebSite.ViewModels;
using WebSite.ViewModels.DetailsViewModels;
using WebSite.ViewModels.EditViewModels;
using WebSite.ViewModels.IndexViewModels;
using Attribute = ControleRepresentante.Logic.Models.Attribute;

namespace ControleRepresentante.UnitTests.Controllers
{
    [TestClass]
    public class CRUDControllerBaseTests : TestScenario
    {
        const AttributeType AttributeTypeConst = AttributeType.Fabrica;
        
        private MockRepository m_MockRepository;
        private MockModel m_MockModel;

        protected override void RegisterRepositories(RepositoryProvider<ControleRepresentanteCatalog> repositoryProvider)
        {
            base.RegisterRepositories(repositoryProvider);

            m_MockModel = new MockModel();
            m_MockRepository = new MockRepository(m_MockModel);
            repositoryProvider.RegisterRepository(m_MockRepository);
        }

        [TestMethod]
        public void TestCreatePage()
        {
            MockController mockController = CreateMockController();
            ActionResult actionResult = mockController.Create();
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult) actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(CRUDViewModel));
            CRUDViewModel crudViewModel = (CRUDViewModel) viewResult.Model;
            Assert.IsNull(crudViewModel.Message);
            Assert.IsInstanceOfType(crudViewModel.Model, typeof(MockModel));
            Assert.AreEqual(crudViewModel.Model, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
        }

        private MockController CreateMockController()
        {
            this.LoginAsAdmin();
            this.CreateRepresentante("repre", true, "u");
            Representante representante;
            m_MockRepository.User = RepositoryProvider.GetRepository<IUserRepository>().GetUser("u", "u", out representante);
            m_MockRepository.Representante = representante;
            this.Logout();
            this.LoginAs("u");

            Guid attributeAId = this.CreateAttribute(AttributeTypeConst, "a");
            Guid attributeBId = this.CreateAttribute(AttributeTypeConst, "b");
            Guid attributeCId = this.CreateAttribute(AttributeTypeConst, "c");

            Attribute attribute1 = new Attribute { Name = "a", Type = AttributeTypeConst, Id = attributeAId };
            Attribute attribute2 = new Attribute { Name = "b", Type = AttributeTypeConst, Id = attributeBId };
            Attribute attribute3 = new Attribute { Name = "c", Type = AttributeTypeConst, Id = attributeCId};

            m_MockModel.AddAttributeValue(new AttributeValue { Attribute = attribute1, Value = "1" });
            m_MockModel.AddAttributeValue(new AttributeValue { Attribute = attribute2, Value = "2" });
            m_MockModel.AddAttributeValue(new AttributeValue { Attribute = attribute3, Value = "3" });

            return new MockController(AttributeTypeConst, RepositoryProvider, SessionConfiguration, ControllerExtensionProvider);
        }

        [TestMethod]
        public void TestDetails()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            ActionResult actionResult = mockController.Details(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            Assert.AreEqual(id, m_MockRepository.LastGetModelById);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult) actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(AttributeHolderDetailsViewModel));
            AttributeHolderDetailsViewModel attributeHolderDetailsViewModel = (AttributeHolderDetailsViewModel)viewResult.Model;
            Assert.IsNull(attributeHolderDetailsViewModel.Message);
            Assert.AreSame(m_MockModel, attributeHolderDetailsViewModel.Model);
            MockModel mockModel = (MockModel)attributeHolderDetailsViewModel.Model;
            Dictionary<string, string> attributes = attributeHolderDetailsViewModel.Attributes;
            Assert.AreEqual(mockModel.AttributeValues.Count(), attributes.Count);
            foreach (AttributeValue attributeValue in mockModel.AttributeValues)
            {
                Assert.AreEqual(attributeValue.Value, attributes["_" + attributeValue.Attribute.Name]);
            }
        }

        [TestMethod]
        public void TestEditPage()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            ActionResult actionResult = mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            Assert.AreEqual(id, m_MockRepository.LastGetModelById);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(AttributeHolderEditViewModel));
            AttributeHolderEditViewModel attributeHolderEditViewModel = (AttributeHolderEditViewModel)viewResult.Model;
            Assert.IsNull(attributeHolderEditViewModel.Message);
            Assert.AreSame(m_MockModel, attributeHolderEditViewModel.Model);
            Dictionary<string, string> attributes = attributeHolderEditViewModel.Attributes;
            Assert.AreEqual(m_MockModel.AttributeValues.Count(), attributes.Count);
            foreach (AttributeValue attributeValue in m_MockModel.AttributeValues)
            {
                Assert.AreEqual(attributeValue.Value, attributes["_" + attributeValue.Attribute.Name]);
            }
        }

        [TestMethod]
        public void TestEdit()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            mockController.Edit(id);
            FormCollection formCollection = new FormCollection();
            MockControllerExtension mockControllerExtension = ControllerExtensionProvider.MockControllerExtension;
            mockControllerExtension.TryUpdateErrors.Add("a", "b");
            ModelStateDictionary modelStateDictionary = mockController.CommitEdit(formCollection);
            Assert.AreEqual(m_MockModel, mockControllerExtension.LastSetModel);
            Assert.AreEqual(m_MockModel, mockControllerExtension.LastTryUpdateModel);
            Assert.AreEqual(formCollection, mockControllerExtension.LastTryUpdateFormCollection);
            Assert.AreEqual(1, modelStateDictionary.Count);
            Assert.AreEqual(modelStateDictionary["a"].Errors[0].ErrorMessage, "b");
        }

        [TestMethod]
        public void TestDeletePage()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            ActionResult actionResult = mockController.Delete(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            Assert.AreEqual(id, m_MockRepository.LastGetModelById);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(AttributeHolderDetailsViewModel));
            AttributeHolderDetailsViewModel attributeHolderDetailsViewModel = (AttributeHolderDetailsViewModel)viewResult.Model;
            Assert.IsNull(attributeHolderDetailsViewModel.Message);
            Assert.AreSame(m_MockModel, attributeHolderDetailsViewModel.Model);
            Dictionary<string, string> attributes = attributeHolderDetailsViewModel.Attributes;
            Assert.AreEqual(m_MockModel.AttributeValues.Count(), attributes.Count);
            foreach (AttributeValue attributeValue in m_MockModel.AttributeValues)
            {
                Assert.AreEqual(attributeValue.Value, attributes["_" + attributeValue.Attribute.Name]);
            }
        }

        [TestMethod]
        public void TestDelete()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            ActionResult actionResult = mockController.Delete(id, null);
            Assert.AreEqual(id, m_MockRepository.LastRemoveModelById);
            Assert.IsInstanceOfType(actionResult, typeof(RedirectToRouteResult));
            RedirectToRouteResult redirectToRouteResult = (RedirectToRouteResult)actionResult;
            Assert.AreEqual("Index", redirectToRouteResult.RouteValues["action"]);
        }

        [TestMethod]
        public void TestIndexSearchForm()
        {
            MockController mockController = CreateMockController();
            mockController.AddSearchField(new MockField("My field"));
            ActionResult actionResult = mockController.Index();
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult) actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof (IIndexViewModel));
            IndexViewModel indexViewModel = (IndexViewModel) viewResult.Model;
            FormLayoutViewModel searchFormLayoutViewModel = indexViewModel.GetSearchFormLayoutViewModel(HtmlHelper);
            AssertFields(searchFormLayoutViewModel, "My field", "a", "b", "c", "Cidade", "Bairro");
        }

        private static void AssertFields(FormLayoutViewModel searchFormLayoutViewModel, params string[] fieldCaptions)
        {
            Assert.AreEqual(fieldCaptions.Length, searchFormLayoutViewModel.Fields.Count());
            foreach (string fieldCaption in fieldCaptions)
            {
                Assert.IsTrue(searchFormLayoutViewModel.Fields.Any(x => x.Caption == fieldCaption));
            }
        }

        [TestMethod]
        public void TestIndexPageQuery()
        {
            const int Page = 7;
            TestIndex(Page, new Dictionary<string, string>(), new Dictionary<string, string> { { "test-page", Page.ToString() } });
        }

        [TestMethod]
        public void TestIndexPageRequest()
        {
            const int Page = 7;
            TestIndex(Page, new Dictionary<string, string> { { "page", Page.ToString() } }, new Dictionary<string, string>());
        }

        private void TestIndex(int page, Dictionary<string, string> requestValues, Dictionary<string, string> dictionary)
        {
            MockController mockController = CreateMockController();
            mockController.ControllerContext = new ControllerContext(new MockHttpContext(requestValues, dictionary), new RouteData(), mockController);
            ActionResult actionResult = mockController.Index();
            Assert.AreEqual(page, m_MockRepository.LastPage);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult) actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(MockIndexViewModel));
        }

        [TestMethod]
        public void TestSetFieldsAttribute()
        {
            MockController mockController = CreateMockController();
            Attribute attribute = m_MockModel.AttributeValues.First().Attribute;
            NameValueCollection nameValueCollection = new NameValueCollection
            {
                {IndexViewModel.AttributeKey + attribute.Id, true.ToString()},
            };
            ActionResult actionResult = mockController.SetFields(new FormCollection(nameValueCollection));
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(IndexViewModel));
            IndexColumnsPreference indexColumnsPreference = m_MockRepository.User.IndexColumnsPreferences.FirstOrDefault(x => x.Area == MockController.AreaUsed);
            Assert.IsNotNull(indexColumnsPreference);
            Assert.AreEqual(1, indexColumnsPreference.AttributeIndexColumnPreferences.Count());
            Assert.AreEqual(attribute.Id, indexColumnsPreference.AttributeIndexColumnPreferences.First().Attribute.Id);
            Assert.IsFalse(indexColumnsPreference.BuiltInIndexColumnPreferences.Any());
        }

        [TestMethod]
        public void TestSetFields()
        {
            MockController mockController = CreateMockController();
            NameValueCollection nameValueCollection = new NameValueCollection
            {
                {MockIndexColumns.MockColumn.ToString(), true.ToString()},
            };
            ActionResult actionResult = mockController.SetFields(new FormCollection(nameValueCollection));
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(IndexViewModel));
            IndexColumnsPreference indexColumnsPreference = m_MockRepository.User.IndexColumnsPreferences.FirstOrDefault(x => x.Area == MockController.AreaUsed);
            Assert.IsNotNull(indexColumnsPreference);
            Assert.AreEqual(1, indexColumnsPreference.BuiltInIndexColumnPreferences.Count());
            Assert.AreEqual(MockIndexColumns.MockColumn, indexColumnsPreference.BuiltInIndexColumnPreferences.First().ColumnId);
            Assert.IsFalse(indexColumnsPreference.AttributeIndexColumnPreferences.Any());
        }

        [TestMethod]
        [ExpectedException(typeof(RestrictedAreaException))]
        public void TestSetFieldNoUser()
        {
            MockController mockController = CreateMockController();
            m_MockRepository.User = null;
            mockController.SetFields(null);
        }

        #region Endereco

        [TestMethod]
        public void TestEnderecoSelect()
        {
            MockController mockController = CreateMockController();
            m_MockModel.AddEndereco(new Endereco {Id = Guid.NewGuid()});
            m_MockModel.AddEndereco(new Endereco {Id = Guid.NewGuid()});
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoSelect();
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult) actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel) viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.AreEqual(2, enderecoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Enderecos.Count());
            foreach (Endereco endereco in m_MockModel.Enderecos)
            {
                Assert.AreEqual(1, enderecoViewModels.Count(x => x.Id == endereco.Id));
            }
        }

        [TestMethod]
        public void TestEnderecoInsert()
        {
            MockController mockController = CreateMockController();
            Assert.IsFalse(m_MockModel.Enderecos.Any());
            const EnderecoType EnderecoType = EnderecoType.Residencial;
            const string Rua = "rua";
            const string Numero = "numero";
            const string Complemento = "complemento";
            const string Bairro = "bairro";
            const string CEP = "cep";
            const string Cidade = "cidade";
            const string Estado = "estado";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoInsert(EnderecoType.ToString(), Rua, Numero, Complemento, Bairro, CEP, Cidade, Estado, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.AreEqual(1, enderecoViewModels.Count());
            Assert.AreEqual(1, m_MockModel.Enderecos.Count());
            Endereco endereco = m_MockModel.Enderecos.First();
            Assert.AreEqual(EnderecoType, endereco.Tipo);
            Assert.AreEqual(Rua, endereco.Rua);
            Assert.AreEqual(Numero, endereco.Numero);
            Assert.AreEqual(Complemento, endereco.Complemento);
            Assert.AreEqual(Bairro, endereco.Bairro);
            Assert.AreEqual(CEP, endereco.CEP);
            Assert.AreEqual(Cidade, endereco.Cidade);
            Assert.AreEqual(Estado, endereco.Estado);
            Assert.AreEqual(Observacao, endereco.Observacao);
            Assert.IsTrue(endereco.Principal);
        }

        [TestMethod]
        public void TestEnderecoInsertPrincipal()
        {
            MockController mockController = CreateMockController();
            m_MockModel.AddEndereco(new Endereco{Principal = true});
            const EnderecoType EnderecoType = EnderecoType.Residencial;
            const string Rua = "rua";
            const string Numero = "numero";
            const string Complemento = "complemento";
            const string Bairro = "bairro";
            const string CEP = "cep";
            const string Cidade = "cidade";
            const string Estado = "estado";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoInsert(EnderecoType.ToString(), Rua, Numero, Complemento, Bairro, CEP, Cidade, Estado, Observacao, true);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.AreEqual(2, enderecoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Enderecos.Count());
            Endereco endereco = m_MockModel.Enderecos.FirstOrDefault(x => x.Principal);
            Assert.IsNotNull(endereco);
            Assert.AreEqual(EnderecoType, endereco.Tipo);
            Assert.AreEqual(Rua, endereco.Rua);
            Assert.AreEqual(Numero, endereco.Numero);
            Assert.AreEqual(Complemento, endereco.Complemento);
            Assert.AreEqual(Bairro, endereco.Bairro);
            Assert.AreEqual(CEP, endereco.CEP);
            Assert.AreEqual(Cidade, endereco.Cidade);
            Assert.AreEqual(Estado, endereco.Estado);
            Assert.AreEqual(Observacao, endereco.Observacao);
        }

        [TestMethod]
        public void TestEnderecoInsertNotPrincipal()
        {
            const EnderecoType EnderecoType = EnderecoType.Comercial;
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddEndereco(new Endereco{ Id = id, Tipo = EnderecoType, Principal = true });
            Assert.AreEqual(1, m_MockModel.Enderecos.Count());
            const string Rua = "rua";
            const string Numero = "numero";
            const string Complemento = "complemento";
            const string Bairro = "bairro";
            const string CEP = "cep";
            const string Cidade = "cidade";
            const string Estado = "estado";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoInsert(EnderecoType.ToString(), Rua, Numero, Complemento, Bairro, CEP, Cidade, Estado, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.AreEqual(2, enderecoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Enderecos.Count());
            Endereco endereco = m_MockModel.Enderecos.First(x => x.Rua == Rua);
            Assert.AreEqual(EnderecoType, endereco.Tipo);
            Assert.AreEqual(Rua, endereco.Rua);
            Assert.AreEqual(Numero, endereco.Numero);
            Assert.AreEqual(Complemento, endereco.Complemento);
            Assert.AreEqual(Bairro, endereco.Bairro);
            Assert.AreEqual(CEP, endereco.CEP);
            Assert.AreEqual(Cidade, endereco.Cidade);
            Assert.AreEqual(Estado, endereco.Estado);
            Assert.AreEqual(Observacao, endereco.Observacao);
            Assert.IsFalse(endereco.Principal);
        }

        [TestMethod]
        public void TestEnderecoInsertEmptyRuaNumeroCidadeEstado()
        {
            MockController mockController = CreateMockController();
            Assert.IsFalse(m_MockModel.Enderecos.Any());
            const EnderecoType EnderecoType = EnderecoType.Residencial;
            const string Rua = "";
            const string Numero = "";
            const string Complemento = "complemento";
            const string Bairro = "bairro";
            const string CEP = "cep";
            const string Cidade = "";
            const string Estado = "";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoInsert(EnderecoType.ToString(), Rua, Numero, Complemento, Bairro, CEP, Cidade, Estado, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.IsFalse(m_MockModel.Enderecos.Any());
            Assert.IsFalse(enderecoViewModels.Any());
        }

        [TestMethod]
        public void TestEnderecoDelete()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddEndereco(new Endereco {Id = id, Principal = true});
            m_MockModel.AddEndereco(new Endereco {Id = Guid.NewGuid()});
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoDelete(m_MockModel.Enderecos.First().Id);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.AreEqual(1, m_MockModel.Enderecos.Count());
            Assert.AreEqual(1, enderecoViewModels.Count());
            Assert.IsTrue(enderecoViewModels.First().Principal);
            Assert.IsTrue(m_MockModel.Enderecos.First().Principal);
        }

        [TestMethod]
        public void TestEnderecoUpdate()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddEndereco(new Endereco {Id = id});
            const EnderecoType EnderecoType = EnderecoType.Residencial;
            const string Rua = "rua";
            const string Numero = "numero";
            const string Complemento = "complemento";
            const string Bairro = "bairro";
            const string CEP = "cep";
            const string Cidade = "cidade";
            const string Estado = "estado";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoUpdate(m_MockModel.Enderecos.First().Id, EnderecoType.ToString(), Rua, Numero, Complemento, Bairro, CEP, Cidade, Estado, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.AreEqual(1, enderecoViewModels.Count());
            Assert.AreEqual(1, m_MockModel.Enderecos.Count());
            Endereco endereco = m_MockModel.Enderecos.First();
            Assert.AreEqual(id, endereco.Id);
            Assert.AreEqual(EnderecoType, endereco.Tipo);
            Assert.AreEqual(Rua, endereco.Rua);
            Assert.AreEqual(Numero, endereco.Numero);
            Assert.AreEqual(Complemento, endereco.Complemento);
            Assert.AreEqual(Bairro, endereco.Bairro);
            Assert.AreEqual(CEP, endereco.CEP);
            Assert.AreEqual(Cidade, endereco.Cidade);
            Assert.AreEqual(Estado, endereco.Estado);
            Assert.AreEqual(Observacao, endereco.Observacao);
            Assert.IsTrue(endereco.Principal);
        }

        [TestMethod]
        public void TestEnderecoUpdateNotPrincipal()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            const EnderecoType EnderecoType = EnderecoType.Outro;
            m_MockModel.AddEndereco(new Endereco { Id = id });
            m_MockModel.AddEndereco(new Endereco { Id = Guid.NewGuid(), Principal = true, Tipo = EnderecoType });
            const string Rua = "rua";
            const string Numero = "numero";
            const string Complemento = "complemento";
            const string Bairro = "bairro";
            const string CEP = "cep";
            const string Cidade = "cidade";
            const string Estado = "estado";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.EnderecoUpdate(m_MockModel.Enderecos.First().Id, EnderecoType.ToString(), Rua, Numero, Complemento, Bairro, CEP, Cidade, Estado, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<EnderecoViewModel> enderecoViewModels = gridModel.Data.Cast<EnderecoViewModel>();
            Assert.AreEqual(2, enderecoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Enderecos.Count());
            Endereco endereco = m_MockModel.Enderecos.First();
            Assert.AreEqual(id, endereco.Id);
            Assert.AreEqual(EnderecoType, endereco.Tipo);
            Assert.AreEqual(Rua, endereco.Rua);
            Assert.AreEqual(Numero, endereco.Numero);
            Assert.AreEqual(Complemento, endereco.Complemento);
            Assert.AreEqual(Bairro, endereco.Bairro);
            Assert.AreEqual(CEP, endereco.CEP);
            Assert.AreEqual(Cidade, endereco.Cidade);
            Assert.AreEqual(Estado, endereco.Estado);
            Assert.AreEqual(Observacao, endereco.Observacao);
            Assert.IsFalse(endereco.Principal);
        }

        #endregion

        #region Contato

        [TestMethod]
        public void TestContatoSelect()
        {
            MockController mockController = CreateMockController();
            m_MockModel.AddContato(new Contato{Id = Guid.NewGuid()});
            m_MockModel.AddContato(new Contato{Id = Guid.NewGuid()});
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoSelect();
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult) actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel) viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.AreEqual(2, contatoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contatos.Count());
            foreach (Contato contato in m_MockModel.Contatos)
            {
                Assert.AreEqual(1, contatoViewModels.Count(x => x.Id == contato.Id));
            }
        }

        [TestMethod]
        public void TestContatoInsert()
        {
            MockController mockController = CreateMockController();
            Assert.IsFalse(m_MockModel.Contatos.Any());
            const ContatoType ContatoType = ContatoType.Nextel;
            const string Valor = "valor";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoInsert(ContatoType.ToString(), Valor, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.AreEqual(1, contatoViewModels.Count());
            Assert.AreEqual(1, m_MockModel.Contatos.Count());
            Contato contato = m_MockModel.Contatos.ElementAt(0);
            Assert.AreEqual(ContatoType, contato.Tipo);
            Assert.AreEqual(Valor, contato.Valor);
            Assert.AreEqual(Observacao, contato.Observacao);
            Assert.IsTrue(contato.Principal);
        }

        [TestMethod]
        public void TestContatoInsertPrincipal()
        {
            MockController mockController = CreateMockController();
            const ContatoType ContatoType = ContatoType.Nextel;
            m_MockModel.AddContato(new Contato{Principal = true, Tipo = ContatoType});
            const string Valor = "valor";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoInsert(ContatoType.ToString(), Valor, Observacao, true);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.AreEqual(2, contatoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contatos.Count());
            Assert.AreEqual(1, m_MockModel.Contatos.Count(x => x.Principal));
            Contato contato = m_MockModel.Contatos.First(x => x.Principal);
            Assert.AreEqual(ContatoType, contato.Tipo);
            Assert.AreEqual(Valor, contato.Valor);
            Assert.AreEqual(Observacao, contato.Observacao);
        }

        [TestMethod]
        public void TestContatoInsertNotPrincipal()
        {
            const ContatoType ContatoType = ContatoType.Nextel;
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddContato(new Contato { Id = id, Tipo = ContatoType, Principal = true });
            Assert.AreEqual(1, m_MockModel.Contatos.Count());
            const string Valor = "valor";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoInsert(ContatoType.ToString(), Valor, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.AreEqual(2, contatoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contatos.Count());
            Contato contato = m_MockModel.Contatos.First(x => x.Valor == Valor);
            Assert.AreEqual(ContatoType, contato.Tipo);
            Assert.AreEqual(Valor, contato.Valor);
            Assert.AreEqual(Observacao, contato.Observacao);
            Assert.IsFalse(contato.Principal);
        }

        [TestMethod]
        public void TestContatoInsertEmptyValor()
        {
            MockController mockController = CreateMockController();
            Assert.IsFalse(m_MockModel.Contatos.Any());
            const ContatoType ContatoType = ContatoType.Nextel;
            const string Valor = "";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoInsert(ContatoType.ToString(), Valor, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.IsFalse(m_MockModel.Contatos.Any());
            Assert.IsFalse(contatoViewModels.Any());
        }

        [TestMethod]
        public void TestContatoDelete()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddContato(new Contato { Id = id, Principal = true});
            m_MockModel.AddContato(new Contato { Id = Guid.NewGuid()});
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoDelete(m_MockModel.Contatos.First().Id);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.AreEqual(1, m_MockModel.Contatos.Count());
            Assert.AreEqual(1, contatoViewModels.Count());
            Assert.IsTrue(m_MockModel.Contatos.First().Principal);
            Assert.IsTrue(contatoViewModels.First().Principal);
        }

        [TestMethod]
        public void TestContatoUpdate()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddContato(new Contato { Id = id });
            const ContatoType ContatoType = ContatoType.Site;
            const string Valor = "valor";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoUpdate(m_MockModel.Contatos.First().Id, ContatoType.ToString(), Valor, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.AreEqual(1, contatoViewModels.Count());
            Assert.AreEqual(1, m_MockModel.Contatos.Count());
            Contato contato = m_MockModel.Contatos.First();
            Assert.AreEqual(id, contato.Id);
            Assert.AreEqual(ContatoType, contato.Tipo);
            Assert.AreEqual(Valor, contato.Valor);
            Assert.AreEqual(Observacao, contato.Observacao);
            Assert.IsTrue(contato.Principal);
        }

        [TestMethod]
        public void TestContatoUpdateNotPrincipal()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            const ContatoType ContatoType = ContatoType.Site;
            m_MockModel.AddContato(new Contato { Id = id });
            m_MockModel.AddContato(new Contato { Id = Guid.NewGuid(), Principal = true, Tipo = ContatoType});
            const string Valor = "valor";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContatoUpdate(m_MockModel.Contatos.First().Id, ContatoType.ToString(), Valor, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContatoViewModel> contatoViewModels = gridModel.Data.Cast<ContatoViewModel>();
            Assert.AreEqual(2, contatoViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contatos.Count());
            Contato contato = m_MockModel.Contatos.First();
            Assert.AreEqual(id, contato.Id);
            Assert.AreEqual(ContatoType, contato.Tipo);
            Assert.AreEqual(Valor, contato.Valor);
            Assert.AreEqual(Observacao, contato.Observacao);
            Assert.IsFalse(contato.Principal);
        }
        
        #endregion

        #region Conta

        [TestMethod]
        public void TestContaSelect()
        {
            MockController mockController = CreateMockController();
            m_MockModel.AddConta(new Conta{Id = Guid.NewGuid()});
            m_MockModel.AddConta(new Conta{Id = Guid.NewGuid()});
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaSelect();
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult) actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel) viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.AreEqual(2, contaViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contas.Count());
            foreach (Conta conta in m_MockModel.Contas)
            {
                Assert.AreEqual(1, contaViewModels.Count(x => x.Id == conta.Id));
            }
        }

        [TestMethod]
        public void TestContaInsert()
        {
            MockController mockController = CreateMockController();
            Assert.IsFalse(m_MockModel.Contas.Any());
            const string Banco = "banco";
            const string Agencia = "agencia";
            const string Numero = "numero";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaInsert(Banco, Agencia, Numero, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.AreEqual(1, contaViewModels.Count());
            Assert.AreEqual(1, m_MockModel.Contas.Count());
            Conta conta = m_MockModel.Contas.First();
            Assert.AreEqual(Banco, conta.Banco);
            Assert.AreEqual(Agencia, conta.Agencia);
            Assert.AreEqual(Numero, conta.Numero);
            Assert.AreEqual(Observacao, conta.Observacao);
            Assert.IsTrue(conta.Principal);
        }

        [TestMethod]
        public void TestContaInsertPrincipal()
        {
            MockController mockController = CreateMockController();
            m_MockModel.AddConta(new Conta{Principal = true});
            const string Banco = "banco";
            const string Agencia = "agencia";
            const string Numero = "numero";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaInsert(Banco, Agencia, Numero, Observacao, true);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.AreEqual(2, contaViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contas.Count());
            Conta conta = m_MockModel.Contas.FirstOrDefault(x => x.Principal);
            Assert.IsNotNull(conta);
            Assert.AreEqual(Banco, conta.Banco);
            Assert.AreEqual(Agencia, conta.Agencia);
            Assert.AreEqual(Numero, conta.Numero);
            Assert.AreEqual(Observacao, conta.Observacao);
        }

        [TestMethod]
        public void TestContaInsertNotPrincipal()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddConta(new Conta { Id = id, Principal = true });
            Assert.AreEqual(1, m_MockModel.Contas.Count());
            const string Banco = "banco";
            const string Agencia = "agencia";
            const string Numero = "numero";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaInsert(Banco, Agencia, Numero, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.AreEqual(2, contaViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contas.Count());
            Conta conta = m_MockModel.Contas.First(x => x.Numero == Numero);
            Assert.AreEqual(Banco, conta.Banco);
            Assert.AreEqual(Agencia, conta.Agencia);
            Assert.AreEqual(Numero, conta.Numero);
            Assert.AreEqual(Observacao, conta.Observacao);
            Assert.IsFalse(conta.Principal);
        }

        [TestMethod]
        public void TestContaInsertEmptyBancoAgenciaNumero()
        {
            MockController mockController = CreateMockController();
            Assert.IsFalse(m_MockModel.Contas.Any());
            const string Banco = "";
            const string Agencia = "";
            const string Numero = "";
            const string Observacao = "observacao";
            mockController.Edit(Guid.NewGuid());
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaInsert(Banco, Agencia, Numero, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.IsFalse(m_MockModel.Contas.Any());
            Assert.IsFalse(contaViewModels.Any());
        }

        [TestMethod]
        public void TestContaDelete()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddConta(new Conta { Id = id, Principal = true});
            m_MockModel.AddConta(new Conta { Id = Guid.NewGuid() });
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaDelete(m_MockModel.Contas.First().Id);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.AreEqual(1, m_MockModel.Contas.Count());
            Assert.AreEqual(1, contaViewModels.Count());
            Assert.IsTrue(m_MockModel.Contas.First().Principal);
            Assert.IsTrue(contaViewModels.First().Principal);
        }

        [TestMethod]
        public void TestContaUpdate()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddConta(new Conta { Id = id });
            const string Banco = "banco";
            const string Agencia = "agencia";
            const string Numero = "numero";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaUpdate(m_MockModel.Contas.First().Id, Banco, Agencia, Numero, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.AreEqual(1, contaViewModels.Count());
            Assert.AreEqual(1, m_MockModel.Contas.Count());
            Conta conta = m_MockModel.Contas.First();
            Assert.AreEqual(id, conta.Id);
            Assert.AreEqual(Banco, conta.Banco);
            Assert.AreEqual(Agencia, conta.Agencia);
            Assert.AreEqual(Numero, conta.Numero);
            Assert.AreEqual(Observacao, conta.Observacao);
            Assert.IsTrue(conta.Principal);
        }

        [TestMethod]
        public void TestContaUpdateNotPrincipal()
        {
            MockController mockController = CreateMockController();
            Guid id = Guid.NewGuid();
            m_MockModel.AddConta(new Conta { Id = id });
            m_MockModel.AddConta(new Conta { Id = Guid.NewGuid(), Principal = true });
            const string Banco = "banco";
            const string Agencia = "agencia";
            const string Numero = "numero";
            const string Observacao = "observacao";
            mockController.Edit(id);
            Assert.AreEqual(m_MockModel, ControllerExtensionProvider.MockControllerExtension.LastSetModel);
            ActionResult actionResult = mockController.ContaUpdate(m_MockModel.Contas.First().Id, Banco, Agencia, Numero, Observacao, false);
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)actionResult;
            Assert.IsInstanceOfType(viewResult.Model, typeof(GridModel));
            GridModel gridModel = (GridModel)viewResult.Model;
            IEnumerable<ContaViewModel> contaViewModels = gridModel.Data.Cast<ContaViewModel>();
            Assert.AreEqual(2, contaViewModels.Count());
            Assert.AreEqual(2, m_MockModel.Contas.Count());
            Conta conta = m_MockModel.Contas.First();
            Assert.AreEqual(id, conta.Id);
            Assert.AreEqual(Banco, conta.Banco);
            Assert.AreEqual(Agencia, conta.Agencia);
            Assert.AreEqual(Numero, conta.Numero);
            Assert.AreEqual(Observacao, conta.Observacao);
            Assert.IsFalse(conta.Principal);
        }

        #endregion
    }
}
